﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Ferpect.ComponentModel.Design
{
    public class TextOnlyControl : Control
    {
        private bool textChanged;

        public TextOnlyControl()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.Opaque, true);
            this.BackColor = Color.Transparent;
            this.Width = 64;
            this.Height = 18;
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                this.OnTextChanged(EventArgs.Empty);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            this.textChanged = true;
            this.Invalidate();
            base.OnTextChanged(e);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.textChanged)
            {
                Rectangle rc = new Rectangle(this.Location, this.Size);
                if (this.Parent != null)
                {
                    this.Parent.Invalidate(rc, true);
                }
                this.textChanged = false;
            }
            else
            {
                string text = this.Text;
                while (true)
                {
                    SizeF size = e.Graphics.MeasureString(text, this.Font);
                    if (size.Width > this.Width)
                    {
                        text = text.Substring(0, text.Length - 4) + "...";
                        continue;
                    }

                    float x = ((float)this.Width - size.Width) / 2f;
                    float y = ((float)this.Height - size.Height) / 2f;

                    using (Pen p = new Pen(Color.Yellow, 2))
                    {
                        e.Graphics.DrawString(text, this.Font, p.Brush, new PointF(x, y));
                    }
                    break;
                }
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // TextOnly
            // 
            this.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ResumeLayout(false);
        }
    }
}
